import {observable, configure, action} from 'mobx';
import axios from 'axios';

configure({enforceActions: 'observed'});
class Login {
  @observable
  isLogin = true;

  @observable
  phone = '';

  @observable
  password = '';

  @observable
  nickName = '';

  @action.bound
  login () {
    this.isLogin = true;
  }

  @action.bound
  register () {
    this.isLogin = false;
  }

  @action.bound
  changePhone (event) {
    this.phone = event.target.value
  }

  @action.bound
  changePwd (event) {
    this.password = event.target.value
  }

  @action.bound
  changeNickName (event) {
    this.nickName = event.target.value
  }

  @action.bound
  async submitLogin () {
    console.log('登录', this.phone, this.password)
    const { data } = await axios.post('https://conduit.productionready.io/users/login', {
      "user":{    
        "email": this.phone,   
        "password": this.password
      }
    })
    console.log('登录接口返回', data)
  }

  @action.bound
  async submitRegister () {
    console.log('注册', this.nickName, this.phone, this.password)
    const { data } = await axios.post('https://conduit.productionready.io/users', {
      "user":{   
          "username": this.nickName,
          "email": this.phone,
          "password": this.password
      }
    })
    console.log('注册接口返回', data)
  }
}
const login = new Login();
export default login